package com.zhuanzhuan.hero.user.helper;

import com.bj58.youpin.common.utils.json.JsonUtils;
import com.bj58.zhuanzhuan.zzentry.common.service.entity.ServiceHeaderEntity;
import com.bj58.zhuanzhuan.zzentry.common.service.entity.ZZOpenScfBaseResult;
import com.zhuanzhuan.hero.biz.common.util.WxWarnTemplateUtil;
import com.zhuanzhuan.hero.member.enums.MemberStatusEnum;
import com.zhuanzhuan.hero.member.facade.IAuthServiceFacade;
import com.zhuanzhuan.hero.member.facade.IMemberServiceFacade;
import com.zhuanzhuan.hero.member.response.MemberDTO;
import com.zhuanzhuan.hero.member.response.MemberLevelInfoDTO;
import com.zhuanzhuan.hero.user.consts.Consts;
import com.zhuanzhuan.hero.user.enums.ExceptionMsgEnum;
import com.zhuanzhuan.hero.user.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName HeroMemberHelper
 * @Description
 * @Author hanjunjun
 * @Date 2024/7/1 20:37
 */
@Component
@Slf4j
public class HeroMemberHelper {

    @Resource
    private IMemberServiceFacade memberServiceFacade;

    @Resource
    private IAuthServiceFacade authServiceFacade;

    /**
     * 获取memberlLevel-memberName map
     *
     * @param
     * @return java.util.Map<java.lang.Integer,java.lang.String>
     * @author  hanjunjun
     * @date 2024/7/1
     **/
    public Map<Integer, String> getMemberCodeNameMap() {
        log.info("queryAllMemberLevel");

        ZZOpenScfBaseResult<List<MemberLevelInfoDTO>> result = memberServiceFacade.queryAllMemberLevel(new ServiceHeaderEntity());
        if (!result.isSuccess()) {
            log.error("queryAllMemberLevel调用失败");
            WxWarnTemplateUtil.warnOutInvokedService(Consts.SERVICE_NAME, Consts.MEMBER_SERVICE_NAME, "getMemberCodeNameMap调用失败");
            throw new BusinessException(ExceptionMsgEnum.BUSINESS_ERROR);
        }

        List<MemberLevelInfoDTO> memberLevelInfos= result.getData();
        if (CollectionUtils.isEmpty(memberLevelInfos)) {
            return new HashMap<>();
        }

        return memberLevelInfos.stream()
            .collect(Collectors.toMap(MemberLevelInfoDTO::getMemberLevel, MemberLevelInfoDTO::getMemberLevelName));
    }

    /**
     * 查询会员信息
     *
     * @param uid
     * @return com.zhuanzhuan.hero.member.entity.Member  可能为空
     * @author  hanjunjun
     * @date 2024/7/6
     **/
    public MemberDTO queryMember(Long uid) {
        log.info("getMemberInfoList uid={}", uid);
        ZZOpenScfBaseResult<List<MemberDTO>> result = memberServiceFacade.queryMemberInfoList(Collections.singletonList(uid));
        log.info("getMemberInfoList result={}", JsonUtils.toJsonWithoutNull(result));
        if (!result.isSuccess()) {
            log.error("getMemberInfo返回失败");
            WxWarnTemplateUtil.warnOutInvokedService(Consts.SERVICE_NAME, Consts.MEMBER_SERVICE_NAME, "queryMember调用失败");
            throw new BusinessException(ExceptionMsgEnum.BUSINESS_ERROR);
        }

        List<MemberDTO> memberList = result.getData();
        if (CollectionUtils.isEmpty(memberList)) {
            return null;
        }
        return memberList.stream()
            .filter(member -> member.getUid().equals(uid)).findFirst().orElse(null);
    }

    /**
     * 批量查询会员信息
     *
     * @param uids
     * @return java.util.List<com.zhuanzhuan.hero.member.response.MemberDTO>
     * @author  hanjunjun
     * @date 2024/7/15
     **/
    public List<MemberDTO> listMember(List<Long> uids) {
        log.info("getMemberInfoList uids={}", uids);
        if (CollectionUtils.isEmpty(uids)) {
            return new ArrayList<>();
        }

        ZZOpenScfBaseResult<List<MemberDTO>> result = memberServiceFacade.queryMemberInfoList(uids);
        log.info("getMemberInfoList result={}", JsonUtils.toJsonWithoutNull(result));
        if (!result.isSuccess()) {
            log.error("getMemberInfo返回失败");
            WxWarnTemplateUtil.warnOutInvokedService(Consts.SERVICE_NAME, Consts.MEMBER_SERVICE_NAME, "listMember调用失败");
            throw new BusinessException(ExceptionMsgEnum.BUSINESS_ERROR);
        }

        return result.getData();
    }

    /**
     * 是否有权限
     *
     * @param uid
     * @param t
     * @param authCode
     * @return boolean
     * @author  hanjunjun
     * @date 2024/7/15
     **/
    public boolean hasAuth(Long uid, String authCode) {
        log.info("hero_member checkAuth uid={}, authCode={}", uid, authCode);
        ZZOpenScfBaseResult<Boolean> result = authServiceFacade.checkAuth(uid, "", authCode);
        log.info("hero_member checkAuth result={}", JsonUtils.toJsonWithoutNull(result));
        if (!result.isSuccess()) {
            throw new BusinessException(ExceptionMsgEnum.BUSINESS_ERROR);
        }

        return result.getData();
    }

    public boolean isMember(MemberDTO member) {
        if (Objects.isNull(member)) {
            return false;
        }
        //只要开通过会员就是会员  区别只是状态不同
        return Objects.nonNull(member.getMemberStatus());
    }

    /**
     * 注销场景判断是否是会员
     *  必须是生效中的
     * @param member
     * @return boolean
     * @author  hanjunjun
     * @date 2024/7/23
     **/
    public boolean isMemberUnregist(MemberDTO member) {
        if (Objects.isNull(member)) {
            return false;
        }
        //必须是生效中的
        return MemberStatusEnum.ACTIVE.getStatus().equals(member.getMemberStatus());
    }
}
